/**
 * Created by xmfang on 2014/12/11.
 */

var mongoose = require('mongoose');
var response = require('./response');
var config = require('../conf/config');
var type = config.constant.CommentOrLog;
var _ = require('underscore');
var errorCode = config.errorCode;
var messageSender = require('./messageSender');

var CommentAndLog = mongoose.model('CommentAndLog');
var Schedule = mongoose.model('Schedule');

exports.query = function (req, res) {
    var start = Number(req.params.start);
    var size =  Number(req.params.size);

    CommentAndLog.find({
        scheduleId : req.params.scheduleId
    }).skip(start).limit(size).sort({createTime: 'desc'}).exec(function (error, results) {
        if (error) {
            console.error('根据日程ID获取相关日志和评论时出错： ' + error);
            response.error(res, errorCode.databaseError);
        } else {
            response.data(res, results);
        }
    });
};

exports.createComment = function (req, res) {
    if (req.body.beReplyCommentId) {
        CommentAndLog.findById(req.body.beReplyCommentId, function (error, comment) {
            if (error) {
                console.error('根据ID查找评论时出错：' + error);
                response.error(res, errorCode.databaseError);
            } else if (!comment) {
                response.data(res, config.errorCode.commentIsNotExist);
            } else {
                createTheComment(req, res);
            }
        });
    } else {
        createTheComment(req, res);
    }
};

var createTheComment = function (req, res) {
    Schedule.findById(req.body.scheduleId, function (error, schedule) {
        if (error) {
            console.error('根据ID查找日程时出错：' + error);
            response.error(res, errorCode.databaseError);
        } else if (!schedule) {
            response.data(res, config.errorCode.scheduleIsNotExist);
        } else {
            var commentAndLog = new CommentAndLog(req.body);
            commentAndLog.type = type.COMMENT;
            commentAndLog.save(function (error, comment) {
                if (error) {
                    console.error('创建评论时出错：' + error);
                    response.error(res, errorCode.databaseError);
                } else {
                    response.data(res, comment);

                    var memberIds = _.pluck(getAcceptAttendees(schedule.attendees, commentAndLog.memberId), 'member');
                    if (memberIds.length > 0) {
                        messageSender.sendCommentMessage(req.body.memberName, schedule, memberIds);
                    }
                }
            });
        }
    });
};

var getAcceptAttendees = function (attendees, memberId) {
    return _.filter(attendees, function (attendee) {
        return attendee.attitude === config.constant.attitude.ACCEPT && attendee.member != memberId;
    });
};

exports.removeComment = function (req, res) {
    CommentAndLog.remove({_id : req.body.commentId}, function (error, data) {
        if (error) {
            console.error('删除评论时出错：' + error);
            response.error(res, errorCode.databaseError);
        } else {
            response.success(res);
        }
    });
};

exports.createLog = function (scheduleId, memberId, actionType, content) {
    var log = {
        scheduleId: scheduleId,
        memberId : memberId,
        type : type.LOG,
        actionType : actionType,
        content : content
    };
    var commentAndLog = new CommentAndLog(log);
    commentAndLog.type = type.LOG;
    commentAndLog.save(function (error, log) {
        if (error) {
            console.error('保存日志时出错：' + error);
        }
    });
};